home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / cool.lha / ice / pisces / par / par.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-04  |  3.8 KB  |  180 lines

  1. /*
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. */
  12.  
  13. #include "par.h"
  14.  
  15. #include <errno.h>
  16. #ifdef VMS
  17. #include <unixio.h>
  18. #endif
  19.  
  20. #ifndef AR_CMD
  21. #ifdef os2
  22. #define AR_CMD "lib /PAGESIZE:64"
  23. #else
  24. #define AR_CMD "ar "
  25. #endif
  26. #endif
  27.  
  28. char    *ar_cmd = AR_CMD;
  29. char    *ar_replace = AR_REPLACE;
  30. char    *ar_delete = AR_DELETE;
  31. char    *ar_add = AR_ADD;
  32. char    *archive = NULL;
  33. char    ar_options[100];
  34. char    *filelist[ MAXFILES ];
  35. char    *ProgramName;
  36.  
  37. main(argc, argv)
  38.     int    argc;
  39.     char    **argv;
  40. {
  41.  
  42.     register char    **fp = filelist;
  43.     register char    **temp;
  44.     register char    *p, *eof;
  45.     char    *fname = NULL;
  46.     char    cmd_line[255];
  47.     char    *bol;
  48.     struct filepointer    *filecontent;
  49.     
  50.     if(argc < 3) {
  51.         fprintf(stderr,"usage: par <archive> [adr] @FILELIST\n");
  52.         exit(1);
  53.     }
  54.  
  55. #ifdef os2
  56.     strcpy (ar_options, "/nologo ");
  57. #endif
  58.     ProgramName = argv[0];
  59.  
  60.     for(argc--, argv++; argc; argc--, argv++) {
  61.         if (**argv == '@') {
  62.             /* all file names are contained in a filename
  63.                following the @-sign (OS/2 only). */
  64.             filecontent = getfile(argv[0]+1);
  65.             p = filecontent->f_p;
  66.             eof = filecontent->f_end;
  67.             while (p < eof) {
  68.                 bol = p;
  69.                 for(; *p != ' ' && *p != '\n' && p < eof;p++);
  70.                 fname = malloc(p-bol+1);
  71.                 *p = '\0';
  72.                 if(isalpha(*bol) || isdigit(*bol)) {
  73.                     strcpy(fname,bol);
  74.                     for (temp = filelist; *temp; temp++)
  75.                         if(strcmp(fname,*temp) == 0)
  76.                             break;
  77.                     if (*temp == NULL)
  78.                         *fp++ = fname;
  79.                 }
  80.                 for(p++; isspace(*p) && p < eof;p++);
  81.             }
  82.             freefile(filecontent);
  83.             break;
  84.         }
  85.         else if (**argv != '-') {
  86.             archive = argv[0];
  87.             continue;
  88.         }
  89.         else switch (*(argv[0]+1)) {
  90.             case 'a':
  91.                 strcat(ar_options, ar_add);
  92.                 strcat(ar_options, " ");
  93.                 break;
  94.             case 'd':
  95.                 strcat(ar_options, ar_delete);
  96.                 strcat(ar_options, " ");
  97.                 break;
  98.             case 'r':
  99.                 strcat(ar_options, ar_replace);
  100.                 strcat(ar_options, " ");
  101.                 break;
  102.             default:
  103.                 do_log("invalid option: -%c\n", *(argv[0]+1));
  104.                 exit(1);
  105.         }
  106.     }
  107.  
  108.  
  109.     /*
  110.      * now peruse through the list of files, passing each to archive
  111.      */
  112.     for(fp=filelist; *fp; fp++) {
  113.         fprintf(stderr, "%s\n",*fp);
  114.         strcpy (cmd_line, ar_cmd);
  115.         strcat (cmd_line, " ");
  116.         strcat (cmd_line, archive);
  117.         strcat (cmd_line, " ");
  118.         strcat (cmd_line, ar_options);
  119.         strcat (cmd_line, " ");
  120.         strcat (cmd_line, *fp);
  121.         strcat (cmd_line, ";");
  122.         system(cmd_line);
  123.     }
  124.     exit(0);
  125. }
  126.  
  127. struct filepointer *getfile(file)
  128.     char    *file;
  129. {
  130.     register long    i;
  131.     struct filepointer    *content;
  132.     struct stat    st;
  133.     FILE   *fd;
  134.  
  135.     content = (struct filepointer *)malloc(sizeof(struct filepointer));
  136.     if ((fd = fopen(file, "r")) == NULL) {
  137.         do_log("cannot open \"%s\" [errno=%d]\n", file, errno);
  138.         content->f_p = content->f_base = content->f_end = malloc(1);
  139.         *content->f_p = '\0';
  140.         return(content);
  141.     }
  142.     stat(file, &st);
  143.     content->f_len = st.st_size+1;
  144.     content->f_base = malloc(content->f_len);
  145.     if (content->f_base == NULL)
  146.       log_fatal("cannot allocate mem\n");
  147.     for (i = 0; i < st.st_size; ++i)
  148.       content->f_base[i] = getc (fd);
  149.     fclose(fd);
  150.     content->f_p = content->f_base;
  151.     content->f_end = content->f_base + st.st_size;
  152.     *content->f_end = '\0';
  153.     content->f_line = 0;
  154.     return(content);
  155. }
  156.  
  157. freefile(fp)
  158.     struct filepointer    *fp;
  159. {
  160.     free(fp->f_base);
  161.     free(fp);
  162. }
  163.  
  164. /*VARARGS*/
  165. log_fatal(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9)
  166. char* x0;
  167. {
  168.     do_log(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9);
  169.     exit (1);
  170. }
  171.  
  172. /*VARARGS0*/
  173. do_log(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9)
  174. char* x0;
  175. {
  176.     fprintf(stderr, "%s:  ", ProgramName);
  177.     fprintf(stderr, x0,x1,x2,x3,x4,x5,x6,x7,x8,x9);
  178. }
  179.  
  180.